package com.digitalchina.lxnsy.activity.management;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;

import com.apkfuns.logutils.LogUtils;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.adapter.SetPermissionAdapter;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.MemberListVO;
import com.digitalchina.lxnsy.entity.PermissionListVO;
import com.digitalchina.lxnsy.toolkit.CustomDialog;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.OkHttpUtil;
import com.digitalchina.lxnsy.xml.AdminXML;
import com.lzy.okhttputils.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity.management
 * 创建人：郑皓
 * 创建时间：2019/4/23 16:54
 * 修改备注：
 * ================================================
 */
public class SetPermissionActivity extends BaseActivity {

    @Bind(R.id.ex_list)
    ExpandableListView exList;
    @Bind(R.id.bt_save)
    Button btSave;

    private List<PermissionListVO> voList = new ArrayList<>();
    private MemberListVO memberListVO;

    private SetPermissionAdapter adapter;
    String array1[] = {"社员管理", "农资经销存", "农事操作", "生产指导", "经营分析", "消息", "销售预估"};
    String array2[][] = {{"社员权限", "基础信息"},
            {"农用品采购", "种子采购", "农资出入库", "出库销售"},
            {"育苗", "定植", "浇水施肥", "中耕除草", "农药使用", "采收"},
            {"生产预测"},
            {"经营情况"},
            {"消息"},
            {"销售预估"}};

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_set_permission);
        setTitle("权限设置");
    }

    @Override
    public void initMember() {

        memberListVO = (MemberListVO) getIntent().getSerializableExtra("MemberListVO");

        adapter = new SetPermissionAdapter();
        exList.setAdapter(adapter);


        getPermission();

    }

    @Override
    public void addListener() {

        exList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                return true;
            }
        });


        btSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                StringBuilder resources = new StringBuilder();
                for (int i = 0; i < adapter.getList().size(); i++) {
                    for (int j = 0; j < adapter.getList().get(i).getChildren().size(); j++) {
                        if (adapter.getList().get(i).getChildren().get(j).getCheckStatus() == 1)
                            resources.append(adapter.getList().get(i).getChildren().get(j).getId()).append(",");
                    }
                }

                LogUtils.e(resources);
                savePermission(resources.toString());
            }
        });

    }

    @Override
    public void setHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                switch (message.what) {
                    case MSG_S_0:
                        voList = FastJsonUtil.getListBean(message.obj.toString(), "resourceList", PermissionListVO.class);

                        adapter.setData(voList);

                        // 默认全部展开
                        for (int i = 0; i < adapter.getGroupCount(); i++) {
                            exList.expandGroup(i);
                        }


                        break;
                    case MSG_F_0:
                        showToast(message.obj.toString());
                        break;
                    case MSG_S_1:
                        CustomDialog.nativeDialogFinish(activity,"保存成功");
                        break;
                    case MSG_F_1:
                        showToast(message.obj.toString());
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    private void getPermission() {

        HttpParams params = new HttpParams();
        params.put("enterpriseId", AdminXML.getEnterpriseId(activity));
        params.put("resNo", AdminXML.getResNo(activity));
        params.put("userAccId", memberListVO.getUserAccId());

        OkHttpUtil.post(activity, URL.URL_GET_MEMBER_PERMISSION, "正在获取权限", params, mHandler,
                MSG_S_0, MSG_F_0);
    }

    private void savePermission(String resources) {

        HttpParams params = new HttpParams();
        params.put("enterpriseId", AdminXML.getEnterpriseId(activity));
        params.put("resNo", AdminXML.getResNo(activity));
        params.put("userAccId", memberListVO.getUserAccId());
        params.put("resources", resources);

        OkHttpUtil.post(activity, URL.URL_SAVE_MEMBER_PERMISSION, "正在保存", params, mHandler,
                MSG_S_1, MSG_F_1);
    }
}
